package linc.cool.pubsub;

import com.rabbitmq.client.*;
import linc.cool.utils.RabbitConstant;
import linc.cool.utils.RabbitUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author yqlin
 * @date 2022/3/3 4:43 下午
 * @description
 */
@Slf4j
public class SinaConsumer {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(RabbitConstant.QUEUE_SINA, false, false, false, null);
        // 绑定交换机
        channel.queueBind(RabbitConstant.QUEUE_SINA, RabbitConstant.EXCHANGE_WEATHER, "");
        channel.basicQos(1);
        channel.basicConsume(RabbitConstant.QUEUE_SINA, false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                log.info("新浪天气收到气象信息: {}",new String(body));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
